package com.play.ballen.home.ui

import android.content.Context
import android.content.Intent
import android.util.Log
import android.view.LayoutInflater
import android.view.View
import android.widget.TextView
import cn.bar.DoubleHeadedDragonBar
import com.play.ballen.R
import com.play.ballen.config.KVConst
import com.play.ballen.databinding.ActivityShaixuanLaoBinding
import com.play.ballen.home.overlay.Constants
import com.play.ballen.home.vm.PLPFragmentVM
import com.yuven.appframework.util.MMKVUtil
import com.yuven.baselib.component.BaseMVVMActivity
import com.zhy.view.flowlayout.FlowLayout
import com.zhy.view.flowlayout.TagAdapter
import com.zhy.view.flowlayout.TagFlowLayout
import kotlin.math.ln

class ShaiXuanGetActivity : BaseMVVMActivity<PLPFragmentVM, ActivityShaixuanLaoBinding>()  {

    var sex: List<String>? = arrayListOf("1","0","0.5","偏1","偏0","其他")
    var like: List<String>? = arrayListOf("猴子","匀称","肌肉","狒狒","熊","猪")
    var other: List<String>? = arrayListOf("无","s","m","双向","Sub","Dom")
    var age_min: Int = 18
    var age_max: Int = 80
    var age_rate = 0.62
    var age_base = 18

    var high_min: Int = 0
    var high_max: Int = 230
    var high_rate = 2.3
    var high_base = 0

    var wight_min: Int = 0
    var wight_max: Int = 200
    var wight_rate = 2
    var wight_base = 0

    val REQUEST_CODE = 100
    val RESULT_GET = 102

    var lat :Double = 0.0
    var lng :Double = 0.0
    var city :String=""
    companion object {
        @JvmStatic
        fun start(context: Context) {
            val starter = Intent(context, ShaiXuanGetActivity::class.java)
            context.startActivity(starter)
        }
    }

    override fun layoutId(): Int {
        return R.layout.activity_shaixuan_lao
    }

    override fun viewModelClass(): Class<PLPFragmentVM> {
        return PLPFragmentVM::class.java
    }

    override fun initView() {
        dataBinding.layoutTitle.ivBtnBack.setOnClickListener{
            finish()
        }
        dataBinding.btnok.setOnClickListener{
            MMKVUtil.encode(KVConst.LAO_CITY, city, false)
            MMKVUtil.encode(KVConst.LAO_LAT, lat, false)
            MMKVUtil.encode(KVConst.LAO_LONG, lng, false)
            finish()
        }

        dataBinding.llCover.setOnClickListener{

        }
        dataBinding.ivCloseAddress.setOnClickListener{
            dataBinding.tvAddress.text = "地点"
            city =""
            lat = 0.00
            lng = 0.00
        }

        dataBinding.layoutTitle.tvTitleCenter.text = "筛选瓶子"

         var city = MMKVUtil.decodeString(KVConst.LAO_CITY, false) ?: ""
        if(city.isNotEmpty()&&city.length>0){
            dataBinding.tvAddress.text = MMKVUtil.decodeString(KVConst.LAO_CITY,false)
        }else{
            dataBinding.tvAddress.text = "地点"
        }


        if(MMKVUtil.decodeInt(KVConst.LAO_AGE_MIN,false)==18&&MMKVUtil.decodeInt(KVConst.LAO_AGE_MAX,false)==80){
            dataBinding.llAge.visibility = View.GONE
            dataBinding.tvAgeBuxian.visibility = View.VISIBLE
            dataBinding.tvAgeMin.text = ""+ age_min
            dataBinding.tvAgeMax.text = ""+ age_max
        }else if(MMKVUtil.decodeInt(KVConst.LAO_AGE_MIN,false)==0&&MMKVUtil.decodeInt(KVConst.LAO_AGE_MAX,false)==0){
            dataBinding.llAge.visibility = View.GONE
            dataBinding.tvAgeBuxian.visibility = View.VISIBLE
        }else{
            age_min = MMKVUtil.decodeInt(KVConst.LAO_AGE_MIN,false)!!
            age_max = MMKVUtil.decodeInt(KVConst.LAO_AGE_MAX,false)!!
            dataBinding.barAge.minValue = ((age_min-age_base)/age_rate).toInt()
            dataBinding.barAge.maxValue = ((age_max-age_base)/age_rate).toInt()
            dataBinding.tvAgeMin.text = ""+ age_min
            dataBinding.tvAgeMax.text = ""+ age_max
            dataBinding.llAge.visibility = View.VISIBLE
            dataBinding.tvAgeBuxian.visibility = View.GONE
        }

        if(MMKVUtil.decodeInt(KVConst.LAO_HEIGHT_MIN,false)==0&&MMKVUtil.decodeInt(KVConst.LAO_HEIGHT_MAX,false)==230){
            dataBinding.llHigh.visibility = View.GONE
            dataBinding.tvHighBuxian.visibility = View.VISIBLE
            dataBinding.tvHighMin.setText(""+ high_min)
            dataBinding.tvHighMax.setText(""+ high_max)
        }else if(MMKVUtil.decodeInt(KVConst.LAO_HEIGHT_MIN,false)==0&&MMKVUtil.decodeInt(KVConst.LAO_HEIGHT_MAX,false)==0){
            dataBinding.llHigh.visibility = View.GONE
            dataBinding.tvHighBuxian.visibility = View.VISIBLE
        }else{
            dataBinding.llHigh.visibility = View.VISIBLE
            dataBinding.tvHighBuxian.visibility = View.GONE

            high_min = MMKVUtil.decodeInt(KVConst.LAO_HEIGHT_MIN,false)!!
            high_max = MMKVUtil.decodeInt(KVConst.LAO_HEIGHT_MAX,false)!!
            dataBinding.barHigh.minValue = ((high_min-high_base)/high_rate).toInt()
            dataBinding.barHigh.maxValue = ((high_max-high_base)/high_rate).toInt()
            dataBinding.tvHighMin.setText(""+ high_min)
            dataBinding.tvHighMax.setText(""+ high_max)
        }


        if(MMKVUtil.decodeInt(KVConst.LAO_WEIGHT_MIN,false)==0&&MMKVUtil.decodeInt(KVConst.LAO_WEIGHT_MAX,false)==200){
            dataBinding.tvWeightMin.setText(""+wight_min)
            dataBinding.tvWeightMax.setText(""+wight_max)
            dataBinding.llWeight.visibility = View.GONE
            dataBinding.tvWeightBuxian.visibility = View.VISIBLE
        }else if(MMKVUtil.decodeInt(KVConst.LAO_WEIGHT_MIN,false)==0&&MMKVUtil.decodeInt(KVConst.LAO_WEIGHT_MAX,false)==0){
            dataBinding.llWeight.visibility = View.GONE
            dataBinding.tvWeightBuxian.visibility = View.VISIBLE
        }else{
            dataBinding.llWeight.visibility = View.VISIBLE
            dataBinding.tvWeightBuxian.visibility = View.GONE
            wight_min = MMKVUtil.decodeInt(KVConst.LAO_WEIGHT_MIN,false)!!
            wight_max = MMKVUtil.decodeInt(KVConst.LAO_WEIGHT_MAX,false)!!
            dataBinding.barWeight.minValue = ((wight_min-wight_base)/wight_rate).toInt()
            dataBinding.barWeight.maxValue = ((wight_max-wight_base)/wight_rate).toInt()
            dataBinding.tvWeightMin.setText(""+wight_min)
            dataBinding.tvWeightMax.setText(""+wight_max)
        }

        if(MMKVUtil.decodeBoolean(KVConst.LAO_SHAIXUAN, false) == true){
            dataBinding.llCover.visibility = View.INVISIBLE

        }else{
            dataBinding.llCover.visibility = View.VISIBLE
        }
        dataBinding.sxBtn.isChecked = MMKVUtil.decodeBoolean(KVConst.LAO_SHAIXUAN, false) == true
        dataBinding.swBtn.isChecked = MMKVUtil.decodeBoolean(KVConst.LAO_IS_BLOCK, false) == true


        dataBinding.flSex.setMaxSelectCount(1)
        val sexAdapter: TagAdapter<String?> = object : TagAdapter<String?>(sex) {
            override fun getView(parent: FlowLayout?, position: Int, t: String?): View {
                val tv = LayoutInflater.from(mContext)
                    .inflate(R.layout.item_label_put, dataBinding.flSex, false) as TextView
                tv.setText(t)
                return tv
            }
        }
        dataBinding.flSex.adapter = sexAdapter
        if(MMKVUtil.decodeInt2(KVConst.LAO_IM,false)!=-1){
            MMKVUtil.decodeInt2(KVConst.LAO_IM,false)?.let { sexAdapter.setSelectedList(it) }
        }
        dataBinding.flSex.setOnTagClickListener(TagFlowLayout.OnTagClickListener { view, position, parent ->
            MMKVUtil.encode(KVConst.LAO_IM, position)
            true
        })

        ///////////////体型
        dataBinding.flLike.setMaxSelectCount(1)
        val likeAdapter: TagAdapter<String?> = object : TagAdapter<String?>(like) {
            override fun getView(parent: FlowLayout?, position: Int, t: String?): View {
                val tv = LayoutInflater.from(mContext)
                    .inflate(R.layout.item_label_get, dataBinding.flLike, false) as TextView
                tv.setText(t)
                return tv
            }
        }
        dataBinding.flLike.setAdapter(likeAdapter)
        if(MMKVUtil.decodeInt2(KVConst.LAO_SHAPE,false)!=-1){
            MMKVUtil.decodeInt2(KVConst.LAO_SHAPE,false)?.let { likeAdapter.setSelectedList(it) }
        }
        dataBinding.flLike.setOnTagClickListener(TagFlowLayout.OnTagClickListener { view, position, parent ->
            MMKVUtil.encode(KVConst.LAO_SHAPE, position)
            true
        })
        ///////////////体型

        ///////////////其他
        dataBinding.flShuxing.setMaxSelectCount(1)
        val otherAdapter: TagAdapter<String?> = object : TagAdapter<String?>(other) {
            override fun getView(parent: FlowLayout?, position: Int, t: String?): View {
                val tv = LayoutInflater.from(mContext)
                    .inflate(R.layout.item_label_get, dataBinding.flShuxing, false) as TextView
                tv.text = t
                return tv
            }
        }
        dataBinding.flShuxing.adapter = otherAdapter
        if(MMKVUtil.decodeInt2(KVConst.LAO_OTHER,false)!=-1){
            MMKVUtil.decodeInt2(KVConst.LAO_OTHER,false)?.let { otherAdapter.setSelectedList(it) }
        }
        dataBinding.flShuxing.setOnTagClickListener(TagFlowLayout.OnTagClickListener { view, position, parent ->
            MMKVUtil.encode(KVConst.LAO_OTHER, position)
            true
        })
        ///////////////其他


        dataBinding.sxBtn.setOnClickListener { view ->
            if ( dataBinding.sxBtn.isChecked) {
                // Switch 开关被打开时执行的操作
                MMKVUtil.encode(KVConst.LAO_SHAIXUAN, true)
                dataBinding.llCover.visibility = View.GONE
            } else {
                dataBinding.llCover.visibility = View.VISIBLE
                MMKVUtil.encode(KVConst.LAO_SHAIXUAN, false)
                // Switch 开关被关闭时执行的操作
            }
        }


        dataBinding.swBtn.setOnClickListener { view ->
            if ( dataBinding.swBtn.isChecked) {
                // Switch 开关被打开时执行的操作
                MMKVUtil.encode(KVConst.LAO_IS_BLOCK, true)
            } else {
                MMKVUtil.encode(KVConst.LAO_IS_BLOCK, false)
                // Switch 开关被关闭时执行的操作
            }
        }

        dataBinding.barAge.setCallBack(object : DoubleHeadedDragonBar.DhdBarCallBack() {
            override fun getMinMaxString(value: Int, value1: Int): String {
                Log.d("xyp","$value~$value1")
                return "$value~$value1"
            }

            override fun getMaxString(value: Int): String {
                Log.d("xyp","$value~getMaxString")
                dataBinding.tvAgeMax.text = ((age_rate*value).toInt()+age_base).toString()
                return super.getMaxString(value)
            }

            override fun getMinString(value: Int): String {
                Log.d("xyp","$value~getMinString")
                dataBinding.tvAgeMin.text = ((age_rate*value).toInt()+age_base).toString()
                return super.getMinString(value)
            }
            override fun onEndTouch(minPercentage: Float, maxPercentage: Float) {
                Log.d("xyp", "min==$minPercentage max==$maxPercentage")
                if(minPercentage.toDouble() ==0.0&& maxPercentage.toDouble() ==100.0){
                    dataBinding.llAge.visibility = View.GONE
                    dataBinding.tvAgeBuxian.visibility = View.VISIBLE
                }else{
                    dataBinding.llAge.visibility = View.VISIBLE
                    dataBinding.tvAgeBuxian.visibility = View.GONE
                }
                age_min = (age_rate*minPercentage).toInt()+age_base
                age_max = (age_rate*maxPercentage).toInt()+age_base
                Log.d("xyp", "min==$age_min max==$age_max")
                dataBinding.tvAgeMin.text = ""+ age_min
                dataBinding.tvAgeMax.text = ""+ age_max
                MMKVUtil.encode(KVConst.LAO_AGE_MIN, age_min)
                MMKVUtil.encode(KVConst.LAO_AGE_MAX, age_max)
            }
        })

        //设置气泡按钮
        var test = LayoutInflater.from(this).inflate(R.layout.toast_view, null) as TextView
        dataBinding.barAge.setToastView(test)
        var test1 = LayoutInflater.from(this).inflate(R.layout.toast_view, null) as TextView
        dataBinding.barAge.setToastView1(test1)


        dataBinding.barHigh.setCallBack(object : DoubleHeadedDragonBar.DhdBarCallBack() {
            override fun getMinMaxString(value: Int, value1: Int): String {
                Log.d("xyp","$value~$value1")
                return "$value~$value1"
            }
            override fun getMaxString(value: Int): String {
                Log.d("xyp","$value~getMaxString")
                dataBinding.tvHighMax.text = ((high_rate*value).toInt()+high_base).toString()
                return super.getMaxString(value)
            }

            override fun getMinString(value: Int): String {
                Log.d("xyp","$value~getMinString")
                dataBinding.tvHighMin.text = ((high_rate*value).toInt()+high_base).toString()
                return super.getMinString(value)
            }

            override fun onEndTouch(minPercentage: Float, maxPercentage: Float) {
                Log.d("xyp","min=="+minPercentage+"max=="+maxPercentage)
                if(minPercentage.toDouble() ==0.0&& maxPercentage.toDouble() ==100.0){
                    dataBinding.llHigh.visibility = View.GONE
                    dataBinding.tvHighBuxian.visibility = View.VISIBLE
                }else{
                    dataBinding.llHigh.visibility = View.VISIBLE
                    dataBinding.tvHighBuxian.visibility = View.GONE
                }

                high_min = (high_rate*minPercentage).toInt()+high_base
                high_max = (high_rate*maxPercentage).toInt()+high_base
                dataBinding.tvHighMin.text = ""+ high_min
                dataBinding.tvHighMax.text = ""+ high_max
                MMKVUtil.encode(KVConst.LAO_HEIGHT_MIN, high_min)
                MMKVUtil.encode(KVConst.LAO_HEIGHT_MAX, high_max)
            }
        })

        var test3 = LayoutInflater.from(this).inflate(R.layout.toast_view, null) as TextView
        dataBinding.barHigh.setToastView(test)
        var test4 = LayoutInflater.from(this).inflate(R.layout.toast_view, null) as TextView
        dataBinding.barHigh.setToastView1(test1)

//        dataBinding.barWeight.maxValue = 120
//        dataBinding.barWeight.minValue = 1
        dataBinding.barWeight.setCallBack(object : DoubleHeadedDragonBar.DhdBarCallBack() {
            override fun getMaxString(value: Int): String {
                Log.d("xyp","$value~getMaxString")
                dataBinding.tvWeightMax.text = ((wight_rate*value).toInt()+wight_base).toString()
                return super.getMaxString(value)
            }

            override fun getMinString(value: Int): String {
                Log.d("xyp","$value~getMinString")
                dataBinding.tvWeightMin.text = ((wight_rate*value).toInt()+wight_base).toString()
                return super.getMinString(value)
            }

            override fun getMinMaxString(value: Int, value1: Int): String {
                Log.d("xyp","$value~$value1")
                return "$value~$value1"
            }
            override fun onEndTouch(minPercentage: Float, maxPercentage: Float) {
                Log.d("xyp","min=="+minPercentage+"max=="+maxPercentage)
                if(minPercentage.toDouble() ==0.0&& maxPercentage.toDouble() ==100.0){
                    dataBinding.llWeight.visibility = View.GONE
                    dataBinding.tvWeightBuxian.visibility = View.VISIBLE
                }else{
                    dataBinding.llWeight.visibility = View.VISIBLE
                    dataBinding.tvWeightBuxian.visibility = View.GONE
                }

                wight_min = (wight_rate*minPercentage).toInt()+wight_base
                wight_max = (wight_rate*maxPercentage).toInt()+wight_base
                dataBinding.tvWeightMin.text = ""+ wight_min
                dataBinding.tvWeightMax.text = ""+ wight_max
                MMKVUtil.encode(KVConst.LAO_WEIGHT_MIN, wight_min)
                MMKVUtil.encode(KVConst.LAO_WEIGHT_MAX, wight_max)
            }
        })

        dataBinding.barWeight.setToastView(test)
        dataBinding.barWeight.setToastView1(test1)
        dataBinding.llLocation.setOnClickListener{
            val intent = Intent(this, MapActivity::class.java)
            intent.putExtra("key", "get");
            startActivityForResult(intent, MapActivity.REQUEST_CODE)
        }

    }

    override fun onActivityResult(requestCode: Int, resultCode: Int, data: Intent?) {
        super.onActivityResult(requestCode, resultCode, data)
        if (resultCode == RESULT_GET && data
            != null
        ) {
            city = data.getStringExtra(Constants.CITY).toString()
            lat  = data.getDoubleExtra(Constants.LAT,0.00)
            lng = data.getDoubleExtra(Constants.LONG,0.00)
            dataBinding.tvAddress.text = city
        }
    }

    override fun initData() {
    }

    override fun initIntent(intent: Intent?) {
    }
}